//
// Created by devink on 8/28/17.
//

#include "decoder.h"
using  namespace dong;

void BaseDecoder::append(const char *str, size_t len) {
    buffer_.append(str,len);
}

std::string BaseDecoder::getRaw() {
    std::string ret;
    ret.swap(buffer_);
    return  ret;
}
std::string BaseDecoder::copyRaw() {
    return  buffer_;
}


int LenDecode::deCode(std::string &msg) {

    if(buffer_.size() == 1 && buffer_[0] == 0x04){
        msg.swap(buffer_);
        return 0;
    }
    int len = buffer_[0];
    if (buffer_.size() < len){
        return  2;
    }
    if(buffer_.size() > 1&& buffer_.size() > len){
        auto beg = buffer_.begin() + 1;
        msg.append(beg, beg + len);
        buffer_.erase(0,len + 1);
        return  1;
    }else{
        return  -1;
    }
}

std::string LenDecode::enCode(std::string &msg) {
    std::string str = std::to_string(msg.length())+ msg;
    return  str;
}

int TagDecoder::deCode(std::string &msg) {
    return 0;
}

std::string TagDecoder::enCode(std::string &msg) {
    return std::__cxx11::string();
}
